package AJAX;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BBSCommand extends HttpServlet {
  private static final long serialVersionUID = 1L; 
    private Map commandMap = new HashMap();

    public void init(ServletConfig config) throws ServletException { 
        String props = config.getInitParameter("property");
        Properties pr = new Properties();
        FileInputStream f = null;
        try{
            f = new FileInputStream(props); 
            pr.load(f);
        }catch (IOException e){
            throw new ServletException(e);
        }finally{
            if(f != null) try { f.close(); } catch(IOException ex) {}
        }
        Iterator keyIter = pr.keySet().iterator();
        while(keyIter.hasNext()){
            String command = (String)keyIter.next();
            String className = pr.getProperty(command);
            try{
                Class commandClass = Class.forName(className);
                Object commandInstance = commandClass.newInstance();
                commandMap.put(command, commandInstance);
            }catch (Exception e){
                System.out.println("예외 발생.");
            }
        }
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		rfid(request,response);		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		rfid(request,response);	
	}
	
	public void rfid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String view = null;
        BBSInter com=null;
		try {
            String command = request.getRequestURI();
            System.out.println(command);
            if (command.indexOf(request.getContextPath()) == 0) {
               command = command.substring(request.getContextPath().length());
            }
            com = (BBSInter)commandMap.get(command);  
            com.human(request, response);
        } catch(Throwable e) {
            throw new ServletException(e);
        }   
    }
}
